<template>
  <div class="layous">
    <div class="mumes">
      <div class="hezi"></div>
    <el-form :model="user" ref="login" :rules="rules">
      <el-form-item label="" prop="userName">
      <el-input placeholder="请输入用户名" v-model="user.userName"/>
    </el-form-item>
    <el-form-item label="" prop="pwd">
      <el-input type="password" placeholder="请输入密码" v-model="user.pwd"/>
    </el-form-item>
    <el-form-item>
      <el-button @click="doLogin">登录</el-button>
    </el-form-item>
  </el-form>
  </div>
  </div>
</template>

<script>
import { userLogin } from '@/api/loggin'
export default {
  name: 'LoginPage',
  data () {
    return {
      rules: {
        userName: [
          {
            required: true,
            message: '用户名必填'
          },
          {
            pattern: /^\w{4,8}$/,
            message: '4-8位数字 字母_组成'
          }
        ],
        pwd: [
          {
            required: true,
            message: '密码必填'
          },
          {
            pattern: /^\w{4,13}$/,
            message: '4-8位数字 字母_组成'
          }
        ]
      },
      user: {
        userName: '',
        pwd: ''
      }
    }
  },
  methods: {
    doLogin () {
      this.$refs.login.validate().then(res => {
        userLogin(this.user).then(res => {
          // 立即缓存
          if (res.data.code === 200) {
            this.$store.commit('user/LOGIN_USER', res.data.data)
            const { token, suerInfo, role } = res.data.data
            localStorage.setItem('token', token)
            localStorage.setItem('suerInfo', JSON.stringify(suerInfo))
            localStorage.setItem('role', role)
            this.$message({
              type: 'success',
              message: '登录成功',
              duration: 2000,
              onClose: () => {
                this.$router.replace('/dashBoard')
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '登录失败'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.layous{
  width: 100vw;
  height: 100vh;
  display: flex;
  background-size: cover;
  background-color: rgba(215, 215, 215, 0.3);
  background-image: url(../../assets/imgs/background.png)
}
.hezi{
  height: 180px;
  background-image: url(../../assets/imgs/logo-b.png);
  background-repeat: no-repeat;
  background-size: 400px
}
.mumes{
  width: 400px;
  height: 400px;
  background-color: rgb(243, 246, 246);
  margin: 260px auto;
}
.el-form{
  width: 300px;
  margin: 0 auto;
}
.el-button {
  width: 150px;
  height: 50px;
  margin: 0 auto;
  color: rgb(243, 246, 246);
  background-color: orangered;
}

</style>
